﻿@namespace ERP.Abp.AntDesignUI
@using System.Text.Json;
@using Microsoft.Extensions.Options
@typeparam TItem
 
<div class="custom-table" id="@_guid">
    @if (SearchTemplate != null)
    {
        <div class="search">
            @SearchTemplate
        </div>
    }


    @if (ToolbarTemplate != null || ToolbarLeftTitleTemplate != null || ToolbarRightButtonTemplate != null)
    {
        <div class="toolbar">
            @if (ToolbarTemplate != null)
            {
            
               @ToolbarTemplate?.Invoke(this)
            }
            else
            {
                
              <span class="title">@ToolbarLeftTitleTemplate?.Invoke(this)</span>
              <AntDesign.Space Class="right"> <AntDesign.SpaceItem>
                  @ToolbarRightButtonTemplate?.Invoke(this)
              </AntDesign.SpaceItem></AntDesign.Space>
            }
        </div>
    }
    <AntDesign.Table @ref="table"
                     OnChange=" OnChange"
                     Loading="@Loading"
                     TItem="TItem"
                     DataSource="@DataSource"
                     Total="@Total"
                     @bind-PageIndex="@PageIndex"
                     @bind-PageSize="@PageSize"
                     ScrollY="@ScrollY"
                     SelectedRows="@SelectedRows"
                     RemoteDataSource="@RemoteDataSource"
                     Size="AntDesign.TableSize.Small"
                     ScrollBarWidth="8"
                   
                     Bordered="true">
        @if (Columns != null)
        {
            foreach (var column in Columns)
            {
                if (column.Actions.Any())
                {
                    <Column TData="object" Title="@column.Title" Fixed="true">
                        <EntityActions TItem="TItem">
                            @foreach (var action in column.Actions)
                            {
                                if (action.ConfirmationMessage != null)
                                {
                                    <EntityAction TItem="TItem"
                                          Color="@(action.Color != null ? action.Color.ToString() : ButtonType.Primary)"
                                          Icon="@action.Icon"
                                          Clicked="async () => await action.Clicked(context)"
                                          ConfirmationMessage="() => action.ConfirmationMessage.Invoke(context)"
                                          Visible="@(action.Visible?.Invoke(context) ?? true)"
                                          Text="@action.Text"
                                          Disabled="@action.Disabled">
                                    </EntityAction>
                                }
                                else
                                {
                                    <EntityAction TItem="TItem"
                                          Clicked="async () => await action.Clicked(context)"
                                          Color="@(action.Color != null ? action.Color.ToString() : ButtonType.Default)"
                                          Icon="@action.Icon"
                                          Visible="@(action.Visible?.Invoke(context) ?? true)"
                                          Text="@action.Text"
                                          Disabled="@action.Disabled">
                                    </EntityAction>
                                }
                            }
                        </EntityActions>
                    </Column>
                }
                else
                {
                    if (column.Component != null)
                    {
                        <Column TData="object" Title="@column.Title">
                           @RenderCustomTableColumnComponent(column.Component, context)
                        </Column>
                    }
                    else
                    {
                       <Column  TData="object" Filterable="@column.Filterable"  SorterMultiple="@column.SorterMultiple"  FilterMultiple="@column.FilterMultiple" Sortable="@column.Sortable"  Align="@column.Align"  Ellipsis="@column.Ellipsis"  TitleTemplate="@column.TitleTemplate"  DataIndex="@column.Data"  Title="@column.Title"  >
                              @(column.ValueConverter == null?
                              GetColumnValue(context, column.Data):
                              GetConvertedFieldValue(context, column) )   
                        </Column>
                    }
                }
            }
        }
        else
        {
            @ColumnsTemplate
        }
        
    </AntDesign.Table>
</div>





